package com.example.opengldemo.sky;

import android.content.Context;
import android.opengl.GLES30;
import android.view.MotionEvent;

import com.example.opengldemo.R;
import com.example.opengldemo._112_.BaseView;
import com.example.opengldemo._112_.Mountion;
import com.example.opengldemo.util.MatrixState;
import com.example.opengldemo.util.TextureUtil;

import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;

public class Sky extends BaseView {

    SkySquare skySquare ;
    SkyBall skyBall ;
    Mountion mountion ;
    public Sky(Context context) {
        super(context);
    }
    private float mPreviousY;
    private float mPreviousX;
    private final float TOUCH_SCALE_FACTOR = 180.0f/360;
    float cx=0;
    float cy=2;
    float cz=24;
    float cr=24;
    float cAngle=0;
    @Override
    public boolean onTouchEvent(MotionEvent e)
    {
        float y = e.getY();
        float x = e.getX();
        switch (e.getAction()) {
            case MotionEvent.ACTION_MOVE:
                float dy = y - mPreviousY;
                float dx = x - mPreviousX;
                cAngle+=dx * TOUCH_SCALE_FACTOR;
                MatrixState.cameraLocation[0]=(float) (Math.sin(Math.toRadians(cAngle))*cr);
                MatrixState.cameraLocation[2]=(float) (Math.cos(Math.toRadians(cAngle))*cr);
                MatrixState.cameraLocation[1]+=dy/10.0f;
        }
        mPreviousY = y;//º«¬º¥•øÿ± Œª÷√
        mPreviousX = x;//º«¬º¥•øÿ± Œª÷√
        return true;
    }
    int[] textureId = new int[10];
    @Override
    public void onSurfaceCreated(GL10 gl10, EGLConfig eglConfig) {
        super.onSurfaceCreated(gl10 , eglConfig);
        GLES30.glEnable(GLES30.GL_DEPTH_TEST);
        //GLES30.glEnable(GLES30.GL_CULL_FACE);
        skySquare = new SkySquare(this) ;
        skyBall = new SkyBall(this) ;
        mountion = new Mountion(this) ;
        textureId[7] = TextureUtil.getTextureId(this , R.mipmap.grass , null, false) ;
        textureId[8] = TextureUtil.getTextureId(this , R.mipmap.rock , null, false) ;

        textureId[0] = TextureUtil.getTextureId(this , R.raw.skycubemap_back , null, false);
        textureId[1] = TextureUtil.getTextureId(this , R.raw.skycubemap_left , null, false);
        textureId[2] = TextureUtil.getTextureId(this , R.raw.skycubemap_right , null, false);
        textureId[3] = TextureUtil.getTextureId(this , R.raw.skycubemap_down , null, false);
        textureId[4] = TextureUtil.getTextureId(this , R.raw.skycubemap_up , null, false);
        textureId[5] = TextureUtil.getTextureId(this , R.raw.skycubemap_front , null, false);
        textureId[6] = TextureUtil.getTextureId(this , R.drawable.sky , null, false);
    }

    @Override
    public void onSurfaceChanged(GL10 gl10, int i, int i1) {
        super.onSurfaceChanged(gl10 , i , i1);
        float des = (float)i/i1 ;
        MatrixState.setProjectFrustum(-des , des  ,-1 , 1 , 1 , 1000 );
    }
    private float UNIT_SIZE = 28f ;
    @Override
    public void onDrawFrame(GL10 gl10) {
        super.onDrawFrame(gl10);
        MatrixState.setCamera(MatrixState.cameraLocation[0],MatrixState.cameraLocation[1],MatrixState.cameraLocation[2],0f,0f,0f,0f,1.0f,0.0f);
        draw2() ;
    }

    public void draw1(){
        final float tzz=0.4f;
        MatrixState.pushMatrix();
        MatrixState.translate(0, 0, -UNIT_SIZE+tzz);
        skySquare.drawSelf(skySquare.shaderHandle.UHandleArray[0]);
        MatrixState.popMatrix();
        //ªÊ÷∆ÃÏø’∫–«∞√Ê
        MatrixState.pushMatrix();
        MatrixState.translate(0, 0, UNIT_SIZE-tzz);
        MatrixState.rotate(180, 0, 1, 0);
        skySquare.drawSelf(skySquare.shaderHandle.UHandleArray[5]);
        MatrixState.popMatrix();
        //ªÊ÷∆ÃÏø’∫–◊Û√Ê
        MatrixState.pushMatrix();
        MatrixState.translate(-UNIT_SIZE+tzz, 0, 0);
        MatrixState.rotate(90, 0, 1, 0);
        skySquare.drawSelf(skySquare.shaderHandle.UHandleArray[1]);
        MatrixState.popMatrix();
        //ªÊ÷∆ÃÏø’∫–”“√Ê
        MatrixState.pushMatrix();
        MatrixState.translate(UNIT_SIZE-tzz, 0, 0);
        MatrixState.rotate(-90, 0, 1, 0);
        skySquare.drawSelf(skySquare.shaderHandle.UHandleArray[2]);
        MatrixState.popMatrix();
        //ªÊ÷∆ÃÏø’∫–œ¬√Ê
        MatrixState.pushMatrix();
        MatrixState.translate(0, -UNIT_SIZE+tzz, 0);
        MatrixState.rotate(-90, 1, 0, 0);
        skySquare.drawSelf(skySquare.shaderHandle.UHandleArray[3]);
        MatrixState.popMatrix();
        //ªÊ÷∆ÃÏø’∫–…œ√Ê
        MatrixState.pushMatrix();
        MatrixState.translate(0, UNIT_SIZE-tzz, 0);
        MatrixState.rotate(90, 1, 0, 0);
        skySquare.drawSelf(skySquare.shaderHandle.UHandleArray[4]);
        MatrixState.popMatrix();
    }
    public void draw2(){
        MatrixState.pushMatrix();
        skyBall.drawSelf(textureId[6]);
        mountion.drawSelf(textureId[7] , textureId[8]);
        MatrixState.popMatrix();}
}
